package cn.tedu.micro.demo.client.exchange;

import cn.tedu.micro.demo.client.RabbitChannelManager;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

/**
 * 先声明组件
 */
public class Declare {

    @Test
    public void directDeclare() throws IOException, InterruptedException, TimeoutException {

        Channel channel = RabbitChannelManager.newChannel();
        channel.queueDeclare("queue1", false, false, false, null);
        channel.queueDeclare("queue2", false, false, false, null);
        channel.queueDeclare("queue3", false, false, false, null);
        //声明交换机 只需要提供2个属性 一个名字 一个类型
        channel.exchangeDeclare("demo_direct", BuiltinExchangeType.DIRECT);
        //声明绑定 将队列绑定到交换机
        channel.queueBind("queue1","demo_direct","北京");
        channel.queueBind("queue2","demo_direct","上海");
        channel.queueBind("queue3","demo_direct","天津");
    }

    @Test
    public void fanoutDeclare() throws IOException, InterruptedException, TimeoutException {

        Channel channel = RabbitChannelManager.newChannel();
        channel.exchangeDeclare("demo_fanout", BuiltinExchangeType.FANOUT);
        //声明绑定 将队列绑定到交换机
        channel.queueBind("queue1","demo_fanout","北京");
        channel.queueBind("queue2","demo_fanout","上海");
        channel.queueBind("queue3","demo_fanout","天津");
    }
    @Test
    public void topicDeclare() throws IOException, InterruptedException, TimeoutException {

        Channel channel = RabbitChannelManager.newChannel();
        //先清空队列
        channel.queueDelete("queue1");
        channel.queueDelete("queue2");
        channel.queueDelete("queue3");
        //重新声明队列
        channel.queueDeclare("queue1", false, false, false, null);
        channel.queueDeclare("queue2", false, false, false, null);
        channel.queueDeclare("queue3", false, false, false, null);
        //声明交换机 只需要提供2个属性 一个名字 一个类型
        channel.exchangeDeclare("demo_topic", BuiltinExchangeType.TOPIC);
        //声明绑定 将队列绑定到交换机
        channel.queueBind("queue1","demo_topic","中国.北京.朝阳");
        channel.queueBind("queue2","demo_topic","*.上海.*");
        channel.queueBind("queue3","demo_topic","中国.#");
    }
    @Test
    public void headersDeclare() throws IOException, InterruptedException, TimeoutException {

        Channel channel = RabbitChannelManager.newChannel();
        //先清空队列
        channel.queueDelete("queue1");
        channel.queueDelete("queue2");
        channel.queueDelete("queue3");
        //重新声明队列
        channel.queueDeclare("queue1", false, false, false, null);
        channel.queueDeclare("queue2", false, false, false, null);
        //声明交换机 只需要提供2个属性 一个名字 一个类型
        channel.exchangeDeclare("demo_header", BuiltinExchangeType.HEADERS);
        //queue1 x-match:any name age
        Map<String,Object> header1=new HashMap<>();
        header1.put("x-match","any");
        header1.put("name","王翠花");
        header1.put("age",18);
        channel.queueBind("queue1","demo_header","",header1);
        //queue2 x-match:all name age
        Map<String,Object> header2=new HashMap<>();
        header2.put("x-match","all");
        header2.put("name","王翠花");
        header2.put("age",18);
        channel.queueBind("queue2","demo_header","",header2);

    }
}
